<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>设备列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <link rel="shortcut icon" href="#"/>
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small" s0tyle="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <!--<input type="hidden" name="unitid" id="unitid">-->
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" placeholder="设备名称" name="name" id="name">
                        </div>

                        <div class="layui-inline layui-show-xs-block">
                            <select name="equipmenttypeid" id="equipmenttypeid">
                                <option value="0">请选择设备类型</option>
                            </select>
                        </div>

                        <div class="layui-inline layui-show-xs-block">
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                    <i class="layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>

                <div class="layui-card-body">
                    <table id="equipmentList" lay-filter="equipmentList"></table>
                </div>

            </div>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn" lay-event="add"><i class="layui-icon"></i>为本单位添加新设备
        </button>
    </div>
</script>

<script>layui.use(['table', 'laydate', 'form', 'layer'],
    function () {
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;

        var unitId = window.location.search.substring(1);
        $("#unitid").prop("value", unitId);
        // alert("unitId="+unitId);
        form.render();
        $.get('equipmentType', function (data) {
            console.log(JSON.stringify(data.data));
            var list = data.data;

            $.each(list, function (index, d) {
                $("#equipmenttypeid").append(new Option(d.typeName, d.id));
            })
            form.render();
        });

        var tableIns = table.render({
            elem: '#equipmentList',
            url: 'equipmentList',
            cellMinWidth: 95,
            page: true,
            where: {"unitid": unitId},
            height: "full-125",
            limit: 10,
            limits: [10, 15, 20, 25],
            id: "equipmentList",
            toolbar: "#toolbarDemo",
            cols: [[
                {type: "checkbox", fixed: "left", width: 50},
                {field: 'eno', title: '设备编号', align: 'center'},
                {field: 'name', title: '设备名称', align: 'center'},
                {field: 'equipmenttypeid', title: '设备类型编号', align: 'center'},
                {field: 'xiumanid', title: '维护人编号', align: 'center'},
                {
                    field: 'status', title: '设备状态', align: 'center', templet: function (data) {
                        if (data.status == "1")
                            return "工作中";
                        else if (data.status == "0")
                            return "已停用";
                    }
                },
                {field: 'pos', title: '设备位置', align: 'center'},
                {field: 'createdate', title: '设备添加时间', align: 'center'},
                {
                    title: '操作', fixed: "right", width: 320, align: "left", templet: function (data) {
                        var str = " <a class='layui-btn ' lay-event='edit'>修改信息</a>";
                        var str1 = "<a class='layui-btn '  lay-event='change'>停用</a>"
                        var str4 = "<a class='layui-btn '  lay-event='change'>启用</a>"
                        var str2 = "<a class='layui-btn '  lay-event='look'>查看维修记录</a>"
                        var str3 = "<a class='layui-btn '  lay-event='del'>报废</a>"
                        if (data.status == 0) {
                            return str + str4 + str2 + str3;
                        }
                        return str + str1 + str2 + str3;
                    }
                }
            ]]
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#createdate' //指定元素
            , trigger: 'click'
        });

        //监听表单的提交事件
        form.on('submit(sreach)', function (data) {
            table.reload("equipmentList", {
                page: {
                    curr: 1
                },
                where: data.field
            });
            return false;
        });

        table.on('tool(equipmentList)', function (obj) {
            switch (obj.event) {
                case "edit":
                    xadmin.open('修改设备信息', 'equipmentUpdate.html?' + obj.data.id, 600, 400);
                    break;
                case "change":
                    /*xadmin.open('增加', 'equipment-add.html?' + obj.data.id, 600, 400);*/
                    //layer.alert("此功能暂时未开放");
                    //alert("obj.data.id:"+obj.data.id+"obj.data.status:"+obj.data.status);
                    $.get("changeStatus", {id: obj.data.id, status: obj.data.status}, function (p) {
                        if (p.code != 0) {
                            layer.alert("异常");
                            return;
                        }
                        if (obj.data.status == 1) {
                            layer.alert("设备已停止工作");
                            tableIns.reload();
                        } else {
                            layer.alert("设备已开始运行");
                            tableIns.reload();
                        }
                    })
                    break;
                case  "look":
                    xadmin.open('查看维修记录', 'baoxiudanList.html?' + obj.data.id, 800, 500);
                    break;
                case "del":
                    var data = obj.data;
                    //layer.confirm
                    layer.confirm('确定报废吗？', function (index) {
                        delEquipment(data, index, obj);
                    });
                    break;
            }

            //向服务端发送删除指令
            function delEquipment(data, index, obj) {
                var id = obj.data.id;
                $.get("delete", {id: id}, function (obj) {
                    if (obj.code != 0) {
                        xadmin.close();
                        return;
                    }
                    layer.close(index);
                    //同步更新表格和缓存对应的值
                    //obj.del();
                    layer.msg("报废成功", {icon: 6});
                    tableIns.reload();
                });
            }
        });
        //头工具栏事件
        table.on('toolbar(equipmentList)',
            function (obj) {
                switch (obj.event) {
                    case 'add':
                        xadmin.open('为单位添加维保人员', 'unitAddEquipment.html?' + unitId, 800, 500);
                        break;
                }
            });
    });</script>

</html>